<?php

namespace App\Http\Controllers;

use Cookie;
use App\Models\User;
use App\Models\Consume;
use App\Models\Client;
use App\Models\AdminUser;
use App\Models\UserQzxinyanQuery;
use App\Models\UserReport;
use App\Models\Setting;
use App\Models\Deduct;
//use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Request;
use zgldh\QiniuStorage\QiniuStorage;
use App\Models\UserXinyan;
use GuzzleHttp\Client as GuzzleClient;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class IdentifyController extends Controller
{
    protected function define_array(){
        return [
            1 => 'verify',//实名认证
            2 => 'carrier',//运营商
            3 => 'bindcard',//银行卡
            4 => 'taobaoweb',//淘宝
            5 => 'sort',//风控
            6 => 'message',//短信
            7 => 'rada',//全景雷达收费项
            8 => 'moxie',//魔蝎
            9 => 'bairong',//百融
            10 => 'fraud',//欺诈报告
            11 => 'duotou',//孟华多头查询
        ];
    }
    //扣手续费
    public function charge($type, $sign, $uid)
    {
        try{
            $map = $this->define_array();
            $nowTime = date('Y-m-d H:i:s');
            $order_sn = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);//订单号
            $client_data = Client::where('sign', $sign)->first();
            if(!count($client_data)) return ['code' => 1, 'text' => '签名不正确'];
            $send_url = $client_data['protocol'].'://'.$client_data['client_url'].'/api/consume/chargeLog';
            $deduct = Deduct::where('sign', $sign)->first()->toArray();
            if(!count($deduct)) return ['code' => 1, '帐户没有设置扣款金额'];
            if ($client_data['cash'] < $deduct[$map[$type]]) return ['code' => 1, 'text' => '业务暂停,余额不足'];
            $is_update = Db::update('update clients set cash=cash - ' . $deduct[$map[$type]] . ' where sign = ?', [$sign]);
            if(!$is_update){
                $consume_data = ['user_id' => $uid,'consume_state'=> 2,'sign' => $sign,'consume_type' => $type, 'consume_time' => $nowTime, 'consume_no' => $order_sn, 'consume_fee' => $deduct[$map[$type]]];
                Db::table('consumes') -> insert($consume_data);
                //写入资方的表中
                $params = [
                    'form_params' => [
                        'user_id' => $uid,
                        'consume_type' => $type,
                        'consume_time' => $nowTime,
                        'consume_no' => $order_sn,
                        'consume_fee' => $deduct[$map[$type]],
                        'consume_state' => 2
                    ]
                ];
                try{
                    $ret = $this->guzzle($send_url, $params);
                    $response = json_decode($ret->getBody()->getContents(), true);
                    @file_put_contents('../storage/logs/send_https_charge_err_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|post='.json_encode($params).'|res='.json_encode($response).PHP_EOL, FILE_APPEND);
                }catch (\Exception $ex){
                    @file_put_contents('../storage/logs/send_https_charge_err_exc_'.date('Ymd').'.log', $ex->getMessage().PHP_EOL, FILE_APPEND);
                }
                return ['code' => 1, 'text' => '扣费失败'];
            }else {
                $consume_data = ['user_id' => $uid,'consume_state'=> 1,'sign' => $sign,'consume_type' => $type, 'consume_time' => $nowTime, 'consume_no' => $order_sn, 'consume_fee' => $deduct[$map[$type]]];
                Db::table('consumes') -> insert($consume_data);
                //写入资方的表中
                $params = [
                    'form_params' => [
                        'user_id' => $uid,
                        'consume_type' => $type,
                        'consume_time' => $nowTime,
                        'consume_no' => $order_sn,
                        'consume_fee' => $deduct[$map[$type]],
                        'consume_state' => 1
                    ]
                ];
                try{
                    $ret = $this->guzzle($send_url, $params);
                    $response = json_decode($ret->getBody()->getContents(), true);
                    @file_put_contents('../storage/logs/send_https_charge_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|post='.json_encode($params).'|res='.json_encode($response).PHP_EOL, FILE_APPEND);
                }catch (\Exception $ex){
                    @file_put_contents('../storage/logs/send_https_charge_exc_'.date('Ymd').'.log', $ex->getMessage().PHP_EOL, FILE_APPEND);
                }
            }
            return ['code' => 0, 'text' => 'ok'];
        }catch (\Exception $exception){
            @file_put_contents('../storage/logs/charge_process_'.date('Ymd').'.log', 'date='.date('Y-m-d').'||type='.$type.'||sign='.$sign.'||uid='.$uid.'||excepton='.$exception->getMessage().PHP_EOL, FILE_APPEND);
            return ['code' => 1, 'text' => '扣费失败'];
        }
    }
    public function rsaSign($args) {
        $args=array_filter($args);//过滤掉空值
        ksort($args);
        $query  =   '';
        foreach($args as $k=>$v){
            if($k=='SignType'){
                continue;
            }
            if($query){
                $query  .=  '&'.$k.'='.$v;
            }else{
                $query  =  $k.'='.$v;
            }
        }
        //这地方不能用 http_build_query  否则会urlencode
        //$query=http_build_query($args);
        $path   =   "../pem/changjie/rsa_private_key.pem";  //私钥地址
        $private_key= file_get_contents($path);
        $pkeyid = openssl_get_privatekey($private_key);
        openssl_sign($query, $sign, $pkeyid);
        openssl_free_key($pkeyid);
        $sign = base64_encode($sign);
        return $sign;
    }
    //guzzle发送数据
    public function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
//            'verify' => 'false',
        ]);
        return $client->request('POST', $url, $params);
    }
    //绑卡的接口
    public function bindCard()
    {
        $data = Request::all();
        $sign = $data['sign'];
        $uid = $data['uid'];
        @file_put_contents('../storage/logs/receive_bindcard_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'||'. json_encode($data).PHP_EOL,FILE_APPEND);
        //扣费
        $bink_res = $this -> charge(config('charge.type.bank_card'), $sign, $uid);
        if($bink_res['code'] == 1) return json_encode($bink_res);
        #调用接口
        return json_encode(['code' => 0, 'text' => '收费成功']);
    }

    //获取资方的taskid和域名放在表里
    public function getTaskidUrl(){
        $data = Request::all();
        $taskid = $data['taskid'];
        $sign = $data['sign'];
        $apiname = $data['apiname'];
        $uid = $data['uid'];
        $insert_data = ['taskid' => $taskid, 'sign' => $sign, 'apiname' => $apiname, 'user_id' => $uid];
        $state = Db::table('user_xinyans') -> insert($insert_data);
        if(!$state) return json_encode(['code' => 1, 'text' => '数据异常，请重试']);
    }
    //淘宝和运营商认证的回调
    public function xyNotify(){
        $data = json_decode(file_get_contents('php://input'), true);
        $taskid = $data['taskId'];
        $token = $data['token'];
        $xinyanData = UserXinyan::where('taskid', $taskid)->first();
        $sign = $xinyanData['sign'];
        $client_url = Client::where('sign', $sign)->first();
        $apiname = $data['apiName'];
        //扣费
        $charge_ret = $this->charge(config('charge.type.'.$apiname),$sign, $xinyanData['user_id']);
        if($charge_ret['code']) return json_encode(['code' => 1, 'text' => '服务暂停']);
        //返回数据到资方服务器
        $send_url = $client_url['protocol'].'://'.$client_url['client_url'].'/api/user/notify';
        $params = [
            'form_params' => [
                'token' => $token,
                'taskId' =>$taskid,
            ]
        ];
        try{
            $this->guzzle($send_url, $params);
            return ['code' => 0, 'text' => 'ok'];
        }catch (\Exception $ex){
            @file_put_contents('./send_url_charge.txt', $ex->getMessage(), FILE_APPEND);
        }
        return ['code' => -1, 'text' => ''];
    }

    //新颜回调通知-报表
    public function reportnotify(Request $request)
    {
        $return['code'] = -1;
        $return['text'] = '';
        $uid = intval(session("uid"));
        $logined = intval(session("logined"));
        $body = file_get_contents('php://input');
        echo "ok";
        $datetime = date("Y-m-d H:i:s");
        @file_put_contents('../storage/logs/reportnotify.log', $datetime . "\n" . json_encode($_GET) . "\n" .$body . "\n" ,FILE_APPEND);

        return $return;

    }
    //实名认证
    public function realMsg(){
        $postData = Request::all();
        $sign = $postData['sign'];
        $uid = $postData['uid'];

        //扣费
        $charge_ret = $this->charge(config('charge.type.verify_name'),$sign, $uid);
        if($charge_ret['code']) return json_encode(['code' => 1, 'text' => '服务暂停']);
        return ['code' => 0, 'info' => 'success'];
    }
    //注册时的发送密码的扣费
    public function sendPassmsg(){
        $receive = Request::all();
        $phpNumbers = json_decode($receive['number'], true);
        $pass = $receive['pass'];
        $product_name = $receive['product_name'];
        $mobile_num = intval(count($phpNumbers));
        if(!$mobile_num) return json_encode(['code' => 1, 'text' => '电话号码不能为空']);
        $phpNumbers_collect = '';
        foreach ($phpNumbers as $num) {
            $phpNumbers_collect .= $num.',';
        }
        $phpNumbers_collect = rtrim($phpNumbers_collect, ',');
        $sign = $receive['sign'];
        $uid = 0;
        //扣费
        try{
            $charge_ret = $this->charge_tips_msg(config('charge.type.message'),$sign, $uid, $mobile_num);
            if($charge_ret['code']) return json_encode(['code' => 1, 'text' => $charge_ret['text']]);
        }catch (\Exception $e){
            @file_put_contents('../storage/logs/sendPassmsg_exc_'.date('Ymd').'.log', date('Y-m-d H:i:s'). $e->getMessage().PHP_EOL, FILE_APPEND);
            die;
        }
        //调用短信接口

        try {
            $tm = date('YmdHms');
            $pw = config('sendtipmsg.pw');
            $data['uid'] = config('sendtipmsg.uid');
            $data['mb']  = $phpNumbers_collect;
            $data['ms']  = str_replace(['点点花','{pass}'], [$product_name, $pass], config('sendtipmsg.passms'));
            $data['tm']  = $tm;
            $data['pw']  = md5($pw . $tm);

            $req_url = "http://".config('sendtipmsg.ip')."/send.do?uid=".$data['uid']."&pw=".config('sendtipmsg.pw')."&mb=".$data['mb']."&ms=".$data['ms']."&ex=77";
            $dataStr     = '';
            foreach ($data as $key => $val) {
                $dataStr .= $key . '=' . $val . '&';
            }
            $send_result = $this->request_post($req_url, $dataStr);
            @file_put_contents('../storage/logs/sendPassmsg_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '&phone='.$phpNumbers_collect.'&sign='.$sign.'&recode='.$send_result.PHP_EOL, FILE_APPEND);
            return ['code' => 0, 'info' => '发送成功'];
        } catch (ClientException $e) {
            @file_put_contents('../storage/logs/sendPassmsg_cexc_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '|client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        } catch (ServerException $e) {
            @file_put_contents('../storage/logs/sendPassmsg_sexc_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '|server'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        }
    }
    //还款提醒的扣费
    public function tipsrepay(){
        $receive = Request::all();
        $phpNumbers = json_decode($receive['number'], true);
        $mobile_num = intval(count($phpNumbers));
        $product_name = $receive['product_name'];
        if(!$mobile_num) return json_encode(['code' => 1, 'text' => '电话号码不能为空']);
        $phpNumbers_collect = '';
        foreach ($phpNumbers as $num) {
            $phpNumbers_collect .= $num.',';
        }
        $phpNumbers_collect = rtrim($phpNumbers_collect, ',');
        $sign = $receive['sign'];
        $send_type = $receive['type'];
        $uid = 0;
        //扣费
        try{
            $charge_ret = $this->charge_tips_msg(config('charge.type.message'),$sign, $uid, $mobile_num);
            @file_put_contents('../storage/jiancelog/send_repay_tips_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|rec='.$send_type.'|mobile='.json_encode($phpNumbers).PHP_EOL, FILE_APPEND);
            if($charge_ret['code']) return json_encode(['code' => 1, 'text' => $charge_ret['text']]);
        }catch (\Exception $e){
            @file_put_contents('../storage/logs/tipsrepay_cexc_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
            die;
        }
        //调用短信接口

        try {
            $tm = date('YmdHms');
            $pw = config('sendtipmsg.pw');
            $data['uid'] = config('sendtipmsg.uid');
            $data['mb']  = $phpNumbers_collect;
            if($send_type == 'today_repay'){
                $data['ms']  = str_replace('北斗星', $product_name, config('sendtipmsg.today_repay_ms'));
            }elseif($send_type == 'tomorrow_repay'){
                $data['ms']  = str_replace('北斗星', $product_name, config('sendtipmsg.tomorrow_repay_ms'));
            }elseif($send_type == 'payout_ms'){
                $data['ms']  = str_replace('北斗星', $product_name, config('sendtipmsg.payout_ms'));
            }else{
                $data['ms']  = str_replace('北斗星', $product_name, config('sendtipmsg.ms'));
            }
            $data['tm']  = $tm;
            $data['pw']  = md5($pw . $tm);

            $req_url = "http://".config('sendtipmsg.ip')."/send.do?uid=".$data['uid']."&pw=".config('sendtipmsg.pw')."&mb=".$data['mb']."&ms=".$data['ms']."&ex=77";
            $dataStr     = '';
            foreach ($data as $key => $val) {
                $dataStr .= $key . '=' . $val . '&';
            }
            $send_result = $this->request_post($req_url, $dataStr);
            @file_put_contents('../storage/logs/tipsrepay_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '&phone='.$phpNumbers_collect.'&sign='.$sign.'&recode='.$send_result.PHP_EOL, FILE_APPEND);
            return ['code' => 0, 'info' => '发送成功'];
        } catch (ClientException $e) {
            @file_put_contents('../storage/logs/tipsrepay_cexc_'.date('Ymd').'.log', 'client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        } catch (ServerException $e) {
            @file_put_contents('../storage/logs/tipsrepay_sexc_'.date('Ymd').'.log', 'server'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        }
    }
    //扣提醒还款时短信提醒的费用的被调用的方法
    public function charge_tips_msg($type, $sign, $uid, $num)
    {
        $map = $this->define_array();
        $nowTime = date('Y-m-d H:i:s');
        $order_sn = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);//订单号
        $client_data = Client::where('sign', $sign)->first();
        if(!count($client_data)) return ['code' => 1, 'text' => '签名不正确'];
        $deduct = Deduct::where('sign', $sign)->first()->toArray();
        if(!count($deduct)) return ['code' => 1, '帐户没有设置扣款金额'];
        $send_url = $client_data['protocol'].'://'.$client_data['client_url'].'/api/consume/chargeLog';
        if ($client_data['cash'] < ($deduct[$map[$type]]*$num)) return ['code' => 1, 'text' => '业务暂停,余额不足'];
        $is_update = Db::update('update clients set cash=cash - ' . $deduct[$map[$type]]*$num. ' where sign = ?', [$sign]);
        if(!$is_update){
            $consume_data = ['user_id' => $uid,'consume_state'=> 2,'sign' => $sign,'consume_type' => $type, 'consume_time' => $nowTime, 'consume_no' => $order_sn, 'consume_fee' => $deduct[$map[$type]]*$num];
            Db::table('consumes') -> insert($consume_data);
            //写入资方的表中
            $params = [
                'form_params' => [
                    'user_id' => $uid,
                    'consume_type' => $type,
                    'consume_time' => $nowTime,
                    'consume_no' => $order_sn,
                    'consume_fee' => $deduct[$map[$type]]*$num,
                    'consume_state' => 2
                ]
            ];
            $this->guzzle($send_url, $params);
            return ['code' => 1, 'text' => '扣费失败'];
        }else {
            $consume_data = ['user_id' => $uid,'consume_state'=> 1,'sign' => $sign,'consume_type' => $type, 'consume_time' => $nowTime, 'consume_no' => $order_sn, 'consume_fee' => $deduct[$map[$type]]*$num];
            Db::table('consumes') -> insert($consume_data);
            //写入资方的表中
            $params = [
                'form_params' => [
                    'user_id' => $uid,
                    'consume_type' => $type,
                    'consume_time' => $nowTime,
                    'consume_no' => $order_sn,
                    'consume_fee' => $deduct[$map[$type]]*$num,
                    'consume_state' => 1
                ]
            ];
            try{
                $ret = $this->guzzle($send_url, $params);
                $response = json_decode($ret->getBody()->getContents(), true);
                @file_put_contents('../storage/logs/charge_tips_msg'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|res='.json_encode($response).PHP_EOL, FILE_APPEND);
            }catch (\Exception $ex){
                @file_put_contents('../storage/logs/charge_tips_msg_exc_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').$ex->getMessage().PHP_EOL, FILE_APPEND);
            }
        }
        return ['code' => 0, 'text' => 'ok'];
    }
     /*
     * post进行url请求
     * @param string $url
     * @param string $param
     */
    public function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl  = $url;
        $curlPost = $param;
        $ch       = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_TIMEOUT,30);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
    //风控的消费明细接口
    public function riskConsume(){

        $map = $this->define_array();
        $receive = Request::all();
        @file_put_contents('../storage/logs/risk_consume_rec_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'||receive='.json_encode($receive).PHP_EOL, FILE_APPEND);
        $type = $receive['type'];//传递的类型
        $consume_type = '';
        if($type == 'moxie'){
            $consume_type = config('charge.type.moxie');
        }elseif($type == 'sort'){
            $consume_type = config('charge.type.sort');
        }elseif($type == 'bairong'){
            $consume_type = config('charge.type.bairong');
        }

        $bsign = $receive['bsign'];
        $mobile = $receive['mobile'];

        $deduct = Deduct::where('bsign', $bsign)->first();
        $client = Client::where('bsign', $bsign)->first();

        $consume_time = date('Y-m-d H:i:s');
        $consume_no = date('ymd') . substr(time(), -5) . substr(microtime(), 2, 5);//订单号
        $consume_fee = $deduct[$map[$consume_type]];
        $consume_state = 1;
        $user_id = $mobile;
        $sign = $client['sign'];

        //保存进自己jcz的表
        $consume = new Consume();
        $consume->consume_type = $consume_type;
        $consume->consume_time = $consume_time;
        $consume->consume_no = $consume_no;
        $consume->consume_fee = $consume_fee;
        $consume->consume_state = $consume_state;
        $consume->user_id = $user_id;
        $consume->sign = $sign;
        $consume->save();
        //保存进业务系统的表
        $send_url = $client['protocol'].'://'.$client['client_url'].'/api/consume/riskchargeLog';
        $params = [
            'form_params' => [
                'user_id' => $mobile,
                'consume_type' => $consume_type,
                'consume_time' => $consume_time,
                'consume_no' => $consume_no,
                'consume_fee' => $consume_fee,
                'consume_state' => 1
            ]
        ];
        try{
            $res = $this->guzzle($send_url, $params);
            $response = json_decode($res->getBody()->getContents());
            @file_put_contents('../storage/logs/risk_consume_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).'||res='.json_encode($response).PHP_EOL, FILE_APPEND);
            return Response()->json(['code' => 0, 'info' => 'success']);
        }catch (\Exception $ex){
            @file_put_contents('../storage/logs/risk_consume_exception_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'||post='.json_encode($params).PHP_EOL, FILE_APPEND);
        }
    }
    //发送短信验证码
    public function send_vcode(){
        $receive = Request::all();
        $mobile = $receive['number'];
        $product_name = $receive['product_name'];
        $vcode = $receive['vcode'];
        $sign = $receive['sign'];
        $uid = 0;
        //扣费
        try{
            $charge_ret = $this->charge_tips_msg(config('charge.type.message'),$sign, $uid, 1);
            @file_put_contents('../storage/logs/tipsrepay'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|res'.json_encode($charge_ret).PHP_EOL, FILE_APPEND);
            if($charge_ret['code']) return json_encode(['code' => 1, 'text' => $charge_ret['text']]);
        }catch (\Exception $e){
            @file_put_contents('../storage/logs/tipsrepay_cexc_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
            return json_encode(['code' => 1, 'text' => '发送失败']);
        }
        //调用短信接口

        try {
            $tm = date('YmdHms');
            $pw = config('sendtipmsg.pw');
            $data['uid'] = config('sendtipmsg.uid');
            $data['mb']  = $mobile;
            $data['ms']  = str_replace(['北斗星', '{vcode}'], [$product_name, $vcode], config('sendtipmsg.vcode'));
            $data['tm']  = $tm;
            $data['pw']  = md5($pw . $tm);

            $req_url = "http://".config('sendtipmsg.ip')."/send.do?uid=".$data['uid']."&pw=".config('sendtipmsg.pw')."&mb=".$data['mb']."&ms=".$data['ms']."&ex=77";
            $dataStr     = '';
            foreach ($data as $key => $val) {
                $dataStr .= $key . '=' . $val . '&';
            }
            $send_result = $this->request_post($req_url, $dataStr);
            @file_put_contents('../storage/logs/send_vocde_msg_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '||ms='.$data['ms'].'&phone='.$mobile.'&sign='.$sign.'&recode='.$send_result.PHP_EOL, FILE_APPEND);
            return ['code' => 0, 'info' => '发送成功'];
        } catch (ClientException $e) {
            @file_put_contents('../storage/logs/send_vocde_msg_cexc_'.date('Ymd').'.log', 'client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        } catch (ServerException $e) {
            @file_put_contents('../storage/logs/send_vocde_msg_sexc_'.date('Ymd').'.log', 'server'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        }
    }

    //用与业务系统公共的发短信接口
    public function general_send_vcode(){
        $receive = Request::all();
        $mobile = $receive['number'];
        $product_name = $receive['product_name'];
        $vcode = $receive['vcode'];
        $sign = $receive['sign'];
        $uid = 0;
        //扣费
        try{
            $charge_ret = $this->charge_tips_msg(config('charge.type.message'),$sign, $uid, 1);
            if($charge_ret['code']) return json_encode(['code' => 1, 'text' => $charge_ret['text']]);
        }catch (\Exception $e){
            @file_put_contents('../storage/logs/tipsrepay_cexc_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
            return json_encode(['code' => 1, 'text' => '发送失败']);
        }
        //调用短信接口

        try {
            $tm = date('YmdHms');
            $pw = config('sendtipmsg.pw');
            $data['uid'] = config('sendtipmsg.uid');
            $data['mb']  = $mobile;
            $data['ms']  = str_replace(['北斗星', '{{account}}'], [$product_name, $vcode], config('sendtipmsg.general_vcode'));
            $data['tm']  = $tm;
            $data['pw']  = md5($pw . $tm);

            $req_url = "http://".config('sendtipmsg.ip')."/send.do?uid=".$data['uid']."&pw=".config('sendtipmsg.pw')."&mb=".$data['mb']."&ms=".$data['ms']."&ex=77";
            $dataStr     = '';
            foreach ($data as $key => $val) {
                $dataStr .= $key . '=' . $val . '&';
            }
            $send_result = $this->request_post($req_url, $dataStr);
            @file_put_contents('../storage/logs/send_vocde_msg_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '||ms='.$data['ms'].'&phone='.$mobile.'&sign='.$sign.'&recode='.$send_result.PHP_EOL, FILE_APPEND);
            return ['code' => 0, 'info' => '发送成功'];
        } catch (ClientException $e) {
            @file_put_contents('../storage/logs/send_vocde_msg_cexc_'.date('Ymd').'.log', 'client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        } catch (ServerException $e) {
            @file_put_contents('../storage/logs/send_vocde_msg_sexc_'.date('Ymd').'.log', 'server'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        }
    }

    //对同一个用户重复打款的短信提醒
    public function general_send_autopayout_vcode(){
        $receive = Request::all();
        $mobile = $receive['number'];
        $product_name = $receive['product_name'];
        $vcode = $receive['vcode'];
        $sign = $receive['sign'];
        $uid = 0;
        //扣费
        try{
            $charge_ret = $this->charge_tips_msg(config('charge.type.message'),$sign, $uid, 1);
            if($charge_ret['code']) return json_encode(['code' => 1, 'text' => $charge_ret['text']]);
        }catch (\Exception $e){
            @file_put_contents('../storage/logs/tipsrepay_cexc_'.date('Ymd').'.log', 'date='.date('Y-m-d H:i:s').'|client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
            return json_encode(['code' => 1, 'text' => '发送失败']);
        }
        //调用短信接口

        try {
            $tm = date('YmdHms');
            $pw = config('sendtipmsg.pw');
            $data['uid'] = config('sendtipmsg.uid');
            $data['mb']  = $mobile;
            $data['ms']  = str_replace(['北斗星', '{{account}}'], [$product_name, $vcode], config('sendtipmsg.auto_payout_toomany'));
            $data['tm']  = $tm;
            $data['pw']  = md5($pw . $tm);

            $req_url = "http://".config('sendtipmsg.ip')."/send.do?uid=".$data['uid']."&pw=".config('sendtipmsg.pw')."&mb=".$data['mb']."&ms=".$data['ms']."&ex=77";
            $dataStr     = '';
            foreach ($data as $key => $val) {
                $dataStr .= $key . '=' . $val . '&';
            }
            $send_result = $this->request_post($req_url, $dataStr);
            @file_put_contents('../storage/logs/send_auto_payout_msg_'.date('Ymd').'.log', date('Y-m-d H:i:s'). '||ms='.$data['ms'].'&phone='.$mobile.'&sign='.$sign.'&recode='.$send_result.PHP_EOL, FILE_APPEND);
            return ['code' => 0, 'info' => '发送成功'];
        } catch (ClientException $e) {
            @file_put_contents('../storage/logs/send_auto_payout_msg_cexc_'.date('Ymd').'.log', 'client'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        } catch (ServerException $e) {
            @file_put_contents('../storage/logs/send_auto_payout_msg_sexc_'.date('Ymd').'.log', 'server'.$e->getErrorMessage().PHP_EOL, FILE_APPEND);
        }
    }

    /**
     * @return false|string
     * 运营商的发短信接口
     */
    //实名认证
    public function carriar_charge(){
        $postData = Request::all();
        $sign = $postData['sign'];
        $uid = $postData['uid'];
        //扣费
        $charge_ret = $this->charge(config('charge.type.carrier'),$sign, $uid);
        @file_put_contents('../storage/chargelogs/carrier_charge_'.date('Ym').'.log', date('Y-m-d H:i:s').'|res:'.json_encode($charge_ret).PHP_EOL, FILE_APPEND);
        if($charge_ret['code']) return json_encode(['code' => 1, 'text' => '服务暂停']);
        return ['code' => 0, 'info' => 'success'];
    }

    /**
     *
     * 反欺诈报告
     */

    public function fraudReport(){
        $postData = Request::all();
        $sign = $postData['sign'];
        $uid = $postData['uid'];
        //扣费
        $charge_ret = $this->charge(config('charge.type.fraud'),$sign, $uid);
        @file_put_contents('../storage/chargelogs/fraud_charge_'.date('Ym').'.log', date('Y-m-d H:i:s').'|res:'.json_encode($charge_ret).PHP_EOL, FILE_APPEND);
        if($charge_ret['code']) return ['code' => 1, 'text' => $charge_ret['text']];
        return ['code' => 0, 'info' => 'success'];
    }

    /**
     *
     * 两个风控的扣费
     */
    public function ZhimiAndMenghuaRisk(){
        $postData = Request::all();
        $sign = $postData['sign'];
        $uid = $postData['uid'];
        //扣费
        $charge_ret = $this->charge(config('charge.type.sort'),$sign, $uid);
        @file_put_contents('../storage/chargelogs/sort_charge_'.date('Ym').'.log', date('Y-m-d H:i:s').'|res:'.json_encode($charge_ret).PHP_EOL, FILE_APPEND);
        if($charge_ret['code']) return ['code' => 1, 'text' => $charge_ret['text']];
        return ['code' => 0, 'info' => 'success'];
    }

    /**
     *
     * 孟华多头的扣费
     */
    public function duotouCharge(){
        $postData = Request::all();
        $sign = $postData['sign'];
        $uid = $postData['uid'];
        //扣费
        $charge_ret = $this->charge(config('charge.type.duotou'),$sign, $uid);
        @file_put_contents('../storage/chargelogs/duotou_charge_'.date('Ym').'.log', date('Y-m-d H:i:s').'|res:'.json_encode($charge_ret).PHP_EOL, FILE_APPEND);
        if($charge_ret['code']) return ['code' => 1, 'text' => $charge_ret['text']];
        return ['code' => 0, 'info' => 'success'];
    }
}